import { withRouter } from 'umi';
import styles from '@/pages/WorkFlow/components/CustomComponents/PostApply/index.less';
import { Card, Form, Input, Spin } from 'antd';
import React, { useEffect, useState } from 'react';
import { getPostApplyData } from '@/pages/WorkFlow/components/CustomComponents/PostApply/service';
import AutoRenderComponent from '@/pages/WorkFlow/components/FormControls/components/AutoRenderComponent/AutoRenderComponent';
import { getManageListAuth_AllData } from '@/pages/Setting/Privilege/services';
import { size } from 'lodash';
import { useRequest } from '@/hooks';
import { PostBasicInfo } from './components/PostForm';

const PostChange = ({ form, wrapperLayout, location, formValueMap = {}, dataSource }) => {
    // console.log(wrapperLayout, 'wrapperLayout');
    const { children: list } = dataSource;
    const { getFieldDecorator } = form;

    const { query } = location;

    const hiDataId = location?.query?.hiDataId || formValueMap?.hiDataId;
    const { bId } = query;
    const [detailLoading, setDetailLoading] = useState(false);
    const [dataArr, setDataArr] = useState(null); // 基础信息
    // const [grateType, setGrateType] = useState(undefined);
    const [commProps, setCommProps] = useState({
        form,
        disabled: true
    });
    // const levelType = {
    //     1: '基础',
    //     2: '职能',
    //     3: '高级'
    // };
    const operationType = {
        0: '新增',
        1: '编辑',
        2: '删除'
    };

    const fetchPostChangeData = async bid => {
        const { code, data } = await getPostApplyData(bid);
        setDetailLoading(true);
        if (code === 10000 && data) {
            setCommProps({ ...commProps });
            const { compareDataField, oldBusinessData } = data;

            // 基础信息 差异数据
            const tips = {};
            if (size(compareDataField?.updateFieldNames)) {
                const { updateFieldNames } = compareDataField;
                // 为权限模块特别处理，后端要求
                updateFieldNames.push('roleCodeList');
                compareDataField.updateFieldNames.forEach(v => {
                    tips[v] = oldBusinessData[v];
                });
            }
            // setGrateType(gradeType);
            setDataArr({ ...data, tips });
        }
        setDetailLoading(false);
    };

    const { data: manageList } = useRequest(getManageListAuth_AllData);
    useEffect(() => {
        if (hiDataId) {
            fetchPostChangeData(hiDataId);
        }
    }, [hiDataId]);

    return (
        <Card bordered={false}>
            <div style={{ position: 'relative' }}>
                <div className={styles.formIntro}>
                    <span>岗位申请流程</span>
                    <div className={styles.info}>以下是岗位详情，请审阅</div>
                </div>
                <Spin spinning={detailLoading}>
                    <Form layout="horizontal" className={styles.postEdit} {...wrapperLayout}>
                        <Form.Item label="申请类型">
                            <>{operationType[dataArr?.operationType]}</>
                        </Form.Item>
                        {list.map(item => {
                            const { label, formId, rules, extra } = item;
                            let { initialValue } = item;
                            // if (formId === 'dutyOrg') {
                            //     initialValue = grateType;
                            // }
                            // if (formId === 'dutyOrgName') {
                            //     initialValue = levelType[grateType];
                            // }
                            if (formId === 'dutyName') {
                                initialValue = dataArr?.dutyAlias;
                            }
                            if (formId === 'operationType') {
                                initialValue = operationType[dataArr?.operationType];
                            }
                            return (
                                <Form.Item
                                    label={label}
                                    key={formId}
                                    extra={extra}
                                    style={{ display: item?.inRenderDom === false ? 'none' : 'block' }}
                                >
                                    {getFieldDecorator(formId, {
                                        initialValue,
                                        rules
                                    })(<AutoRenderComponent form={form} config={item} />)}
                                </Form.Item>
                            );
                        })}
                        <Form.Item style={{ display: 'none' }}>
                            {form.getFieldDecorator('hiDataId', {
                                initialValue: bId || hiDataId
                            })(<Input />)}
                        </Form.Item>
                        <PostBasicInfo
                            data={dataArr}
                            {...commProps}
                            wrapperLayout={wrapperLayout}
                            authData={manageList}
                        />
                    </Form>
                </Spin>
            </div>
        </Card>
    );
};
export default withRouter(PostChange);
